﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class Admin_AdminLogin : System.Web.UI.Page
{
    DataSet DsetAllRegisterdUsers = new DataSet();
    DataSet DsetPaidRegisterdUsers = new DataSet();
    DataSet DsetUnPaidRegisterdUsers = new DataSet();
   
 
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            TDJobcategory.Visible = false;
            TDMsgJobsAppliedByUser.Visible = false;
            //if (Session["AdminUid"] != null && Session["AdminPass"] != null)
            //{
                FillAllRegisteredUsers();
                FillJobCategory();
            //}
            //else
            //{
            //    Response.Redirect("../Admin/Default.aspx");
            //}
        }
        
    }

    public void lnkAllUser_Click(object sender, EventArgs e)
    {
        FillAllRegisteredUsers();
    }
    private void FillJobCategory()
    {
        DataSet DsetJobType = new DataSet();
        JobSiteStarterKit.DAL.DBAccess obj = new JobSiteStarterKit.DAL.DBAccess();
        DsetJobType = obj.ExecuteDataSet("Proc_GetJobcategory");
        if(DsetJobType.Tables.Count>0)
        {
            if(DsetJobType.Tables[0].Rows.Count>0)
            {
                DrpJobCategory.DataSource = DsetJobType.Tables[0].DefaultView;
                DrpJobCategory.DataTextField = "CategoryName";
                DrpJobCategory.DataValueField = "CategoryID";
                DrpJobCategory.DataBind();
            }
        }
       
    }
    private void FillAllRegisteredUsers()
    {
            JobSiteStarterKit.DAL.DBAccess obj = new JobSiteStarterKit.DAL.DBAccess();
            DsetAllRegisterdUsers = obj.ExecuteDataSet("Proc_GetAllRegisteredUser");
            if (DsetAllRegisterdUsers.Tables.Count > 0)
            {
                if (DsetAllRegisterdUsers.Tables[0].Rows.Count > 0)
                {
                    msg.Visible = false;
                    DGrdRegisteredUser.DataSource = DsetAllRegisterdUsers.Tables[0].DefaultView;
                    DGrdRegisteredUser.DataBind();
                    DGrdRegisteredUser.Visible = true;
                    BtnSave.Visible = true;
                }
                else
                {
                    msg.Visible = true;
                    DGrdRegisteredUser.Visible = false;
                    BtnSave.Visible = false;
                }
            }
           
            //if (Cache[System.Web.Configuration.WebConfigurationManager.AppSettings["AllJobSeekers"]] == null)
            //{
            //   Cache.Insert(System.Web.Configuration.WebConfigurationManager.AppSettings["AllJobSeekers"],DsetAllRegisterdUsers,new System.Web.Caching.CacheDependency();
                 
            //}
    }
    private void FillPaidRegisteredUsers()
    {
        JobSiteStarterKit.DAL.DBAccess obj = new JobSiteStarterKit.DAL.DBAccess();
        DsetPaidRegisterdUsers = obj.ExecuteDataSet("Proc_GetPaidRegisteredUser");
        if (DsetPaidRegisterdUsers.Tables.Count > 0)
        {
            if (DsetPaidRegisterdUsers.Tables[0].Rows.Count > 0)
            {
                DGrdRegisteredUser.DataSource = DsetPaidRegisterdUsers.Tables[0].DefaultView;
                DGrdRegisteredUser.DataBind();
                DGrdRegisteredUser.Visible = true;
                BtnSave.Visible = true;
            }
            else
            {
                msg.Visible = true;
                DGrdRegisteredUser.Visible = false;
                BtnSave.Visible = false;

            }
        }
    }
    private void FillUnPaidRegisteredUsers()
    {
        JobSiteStarterKit.DAL.DBAccess obj = new JobSiteStarterKit.DAL.DBAccess();
        DsetUnPaidRegisterdUsers = obj.ExecuteDataSet("Proc_GetUnPaidRegisteredUser");
        if (DsetUnPaidRegisterdUsers.Tables.Count > 0)
        {
            if (DsetUnPaidRegisterdUsers.Tables[0].Rows.Count > 0)
            {
                msg.Visible = false;
                DGrdRegisteredUser.DataSource = DsetUnPaidRegisterdUsers.Tables[0].DefaultView;
                DGrdRegisteredUser.DataBind();
                DGrdRegisteredUser.Visible = true;
                BtnSave.Visible = true;
            }
            else
            {
                msg.Visible = true;
                DGrdRegisteredUser.Visible = false;
                BtnSave.Visible = false;
            }
        }
       
    }
    
    
    protected void DGrdRegisteredUser_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        Control ctrl = e.Row.FindControl("ChkPaid");
        if (ctrl != null)
        {
            CheckBox chk = (CheckBox)ctrl;

            if (DataBinder.Eval(e.Row.DataItem, "paid") != null)
            {
                string value = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "paid"));
                if (value == "1")
                {
                    chk.Checked = true;
                }
            }
            else
            {
                chk.Checked = false;
            }
        }
    }

    protected void BtnSave_Click(object sender, EventArgs e)
    {
        string strunreg;
        JobSiteStarterKit.DAL.DBAccess obj = new JobSiteStarterKit.DAL.DBAccess();
        obj.CommandText = "Proc_UpdatePaymentForRegisteredUser";
        System.Data.SqlClient.SqlParameterCollection obj1=(System.Data.SqlClient.SqlParameterCollection)obj.Parameters;

        string strid=MakeIdString(out strunreg);
        
        obj1.Add("@StrJobSeekerID",SqlDbType.NVarChar,2000).Value=strid;
        obj.ExecuteNonQuery();

        JobSiteStarterKit.DAL.DBAccess objunreg = new JobSiteStarterKit.DAL.DBAccess();
        objunreg.CommandText = "Proc_RemovePaymentForRegisteredUser";
        System.Data.SqlClient.SqlParameterCollection objun = (System.Data.SqlClient.SqlParameterCollection)objunreg.Parameters;

        objun.Add("@StrJobSeekerID", SqlDbType.NVarChar, 2000).Value = strunreg;
        objunreg.ExecuteNonQuery();
    }
    public string MakeIdString(out string strunregistered)
    {
        string Id = "";
        strunregistered = "";
        for (int i = 0; i < DGrdRegisteredUser.Rows.Count - 1; i++)
        {
            Control ctrl = DGrdRegisteredUser.Rows[i].FindControl("chkPaid");
            CheckBox chk = (CheckBox)ctrl;
            if (chk.Checked == true)
            {
                Id = Id + Convert.ToString(DGrdRegisteredUser.DataKeys[i].Value);
                Id = Id + "|";
            }
            else
            {
                strunregistered = strunregistered + Convert.ToString(DGrdRegisteredUser.DataKeys[i].Value); ;
                strunregistered = strunregistered + "|";
            }
        }
        string strid = Id.Substring(0, Id.Length - 1);
        strunregistered = strunregistered.Substring(0, strunregistered.Length - 1);
        return strid;
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (DrpFilter.SelectedItem.Text == "Select Paid Users")
        {
            FillPaidRegisteredUsers();

        }
        else if (DrpFilter.SelectedItem.Text == "Select Unpaid Users")
        {
            FillUnPaidRegisteredUsers();
        }
        else if (DrpFilter.SelectedItem.Text == "Select All Registered User")
        {
            FillAllRegisteredUsers();
        }
    }

    protected void DGrdRegisteredUser_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Email")
        {
            string email = (string)e.CommandArgument;
            Response.Redirect("Adminemail.aspx?email=" + email);
        }
    }

  
    protected void DGrdRegisteredUser_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        DGrdRegisteredUser.PageIndex = e.NewPageIndex;
        if (DrpFilter.SelectedValue == "1")
        { 
            FillAllRegisteredUsers();
        }
        else if (DrpFilter.SelectedValue == "2")
        {
            FillPaidRegisteredUsers();
        }
        else
        {
            FillUnPaidRegisteredUsers();
        }
        
    }
    protected void BtnGetJobcategory_Click(object sender, EventArgs e)
    {
        int JobCatID = Convert.ToInt32(DrpJobCategory.SelectedValue);
        FillJobByJobCategoryID(JobCatID);
         GetUserCategoryWise(JobCatID);
    }

    private void GetUserCategoryWise(int CatID)
    {
        JobSiteStarterKit.DAL.DBAccess obj = new JobSiteStarterKit.DAL.DBAccess();
        obj.AddParameter("@JobCategory", CatID);
        DataSet Dset = obj.ExecuteDataSet("Proc_GetUserCategoryWise");
        if (Dset.Tables.Count > 0)
        {
            DGrdRegisteredUser.DataSource = Dset.Tables[0];
            DGrdRegisteredUser.DataBind();
        }
    }

    private void FillJobByJobCategoryID(int JobID)
    {
        TDJobcategory.Visible = false;
        TDMsgJobsAppliedByUser.Visible = false;
        JobSiteStarterKit.DAL.DBAccess objGet = new JobSiteStarterKit.DAL.DBAccess();
        System.Data.SqlClient.SqlParameterCollection objGet1 = (System.Data.SqlClient.SqlParameterCollection)objGet.Parameters;
        objGet1.Add("@CategoryID",SqlDbType.Int).Value= JobID;
        DataSet DsetJobType = new DataSet();
        //JobSiteStarterKit.DAL.DBAccess obj = new JobSiteStarterKit.DAL.DBAccess();
        DsetJobType = objGet.ExecuteDataSet("Proc_JobsApplyByJOBSEEKER_CategoryWise");
        if (DsetJobType.Tables.Count > 0)
        {
            if (DsetJobType.Tables[0].Rows.Count > 0)
            {
                GrdJobCategory.DataSource = DsetJobType.Tables[0].DefaultView;
                GrdJobCategory.DataBind();
                TDJobcategory.Visible = true;
                TDMsgJobsAppliedByUser.Visible = true;
                TDMsgJobsAppliedByUser.InnerHtml = "Jobs Applied By User As per Job Category";
            }
            else
            {
                TDMsgJobsAppliedByUser.InnerHtml = "No Job Seeker has Applied On this Category,Try another category";
                TDMsgJobsAppliedByUser.Visible = true;
            }
        }
    }
    
    protected void GrdJobCategory_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GrdJobCategory.EditIndex = e.NewPageIndex;
        int JobCatID = Convert.ToInt32(DrpJobCategory.SelectedValue);
        FillJobByJobCategoryID(JobCatID);
    }
    protected void GrdJobCategory_PageIndexChanged(object sender, EventArgs e)
    {

    }
    protected void GrdJobCategory_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        Control ctrl = e.Row.FindControl("ChkP");
        if (ctrl != null)
        {
            CheckBox chk = (CheckBox)ctrl;

            if (DataBinder.Eval(e.Row.DataItem, "paymentStatus") != null)
            {
                string value = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "paymentStatus"));
                if (value == "1")
                {
                    chk.Checked = true;
                }
            }
            else
            {
                chk.Checked = false;
            }
        }
    }
}
